﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
//using UnityEngine.Scripting;

public class Sound_Control : MonoBehaviour
{
    public float MinVolume = -20f;
    public float MaxVolume = 10f;
    static public float DefaultVolume;
    public AudioMixer mixer;
    public Slider slider;
    //public GameObject Scripts;
    
    // Start is called before the first frame update
    protected void Start()
    {
        if (MinVolume < -20f||MinVolume>80f)
        {
            MinVolume = -20f;
        }
        if (MaxVolume > 80f||MaxVolume<-20f)
        {
            MaxVolume = 10f;
        }
        if (MinVolume > MaxVolume)
        {
            float temp = MinVolume;
            MinVolume = MaxVolume;
            MaxVolume = temp;
        }
        if (DefaultVolume > MaxVolume || DefaultVolume < MinVolume)
        {
            DefaultVolume = (MaxVolume + MinVolume) / 2;
        }
        slider.minValue = MinVolume;
        slider.maxValue = MaxVolume;
        slider.value = DefaultVolume;
        SetVolume(DefaultVolume);
        GetComponent<Speaker_Toggle>().SetMin(MinVolume);
        GetComponent<Speaker_Toggle>().SetMax(MaxVolume);
    }
    public void SetVolume(float volume)
    {
        DefaultVolume = volume;
        if (volume == MinVolume)
        {
            mixer.SetFloat("Volume", -80f);
        }
        else
        {
            mixer.SetFloat("Volume", volume);
        }
    }
}
